home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / misc.m < prev    next >
Encoding:
Text File  |  1991-11-01  |  3.2 KB  |  137 lines

  1. // 
  2. // Miscellaneous support functions for Cassandra
  3. // Copyright (c) 1989, 1990, 1991 by Jiro Nakamura. All Rights Reserved.
  4. //
  5. // misc.m
  6. //
  7. //    by Jiro Nakamura (jiro@shaman.com)
  8. //
  9. // RCS Information
  10. // Revision Number->    $Revision: 2.6 $
  11. // Last Revised->    $Date: 91/11/01 17:24:26 $
  12. //
  13. static char rcsid[] = "$Id: misc.m,v 2.6 91/11/01 17:24:26 jiro Exp Locker: jiro $";
  14.  
  15. #ifdef DEBUG
  16.     #undef DEBUG
  17. #endif
  18.  
  19. #import <stdio.h>
  20. #import "cass.h"
  21. #import <strings.h>
  22. #import <appkit/Panel.h>       /* for NXRunAlertPanel */
  23. #import <sys/file.h>
  24. #import <libc.h>            /* for open(), close(), exit() */
  25. #import "misc.h"
  26. #import "errno.h"
  27.  
  28. // Function:     fileOpen(filename, mode, message)
  29. // Arguments:    char * filename -> name of file to be opened
  30. //        char * mode    -> mode using fopen() (e.g. "r", "a+")
  31. //        char * message    -> message to be shown if fopen() fails
  32. // Description:    Opens the file <filename> with mode <mode>
  33. //        using fopen(). If it is successful, a FILE *
  34. //        is returned. If it fails, fileOpen tries
  35. //        to create the file with creat() mode 0600.
  36. //        If that fails too, it uses NXRunAlertPane()
  37. //        to show <message>, then exit()s the program.
  38. // Returns:    FILE * to open file
  39. FILE *fileOpen(filename,mode,message)
  40. char *filename, *mode, *message;
  41. {
  42.     FILE *fd, *fopen();
  43.  
  44.     #ifdef DEBUG
  45.         fprintf(stderr,"fileOpened <%s> with mode %s\n",
  46.                 filename, mode);
  47.     #endif
  48.  
  49.     if( (fd = fopen(filename, mode)) == NULL)
  50.         {
  51.         int ifd;
  52.  
  53.         fprintf(stderr,"%s: Creating file "
  54.                 "%s with mode %s....\n",
  55.                 PROGNAME, filename,mode);
  56.         
  57.         ifd = open( filename, O_CREAT, 0600);
  58.         close(ifd);
  59.         if( (fd = fopen(filename, mode)) == NULL)
  60.             {
  61.             NXRunAlertPanel("File Error",message,"Quit",NULL,NULL);
  62.             exit(1);
  63.             }
  64.         }
  65.     return fd;
  66. }
  67.  
  68. // Seek file to this position
  69. FILE *fileSeek(source, here, errormsg)
  70. FILE *source;
  71. int here;
  72. char *errormsg;
  73. {    
  74.     #ifdef DEBUG
  75.         fprintf(stderr,"Seeking to %d in fileSeek\n",here);
  76.     #endif
  77.  
  78.  
  79.     if(  here < 0)
  80.         {
  81.         fprintf(stderr,"%s: Error in fileSeek, "
  82.             "cannot seek to negative number %d\n", 
  83.             PROGNAME, here);
  84.         NXRunAlertPanel("File Seek Error", 
  85.             "Cannot seek to a negative number.",
  86.             "OK",NULL,NULL);
  87.         return NULL;
  88.         }
  89.     
  90.     /* Seek to the correct position */
  91.     if( fseek( source, (long) FILE_LEN * here, SEEK_SET) == -1)     
  92.         {        
  93.         if( errno == EIO)
  94.             {
  95.             fprintf(stderr, "%s: I/O Error on read. ", PROGNAME);
  96.             NXRunAlertPanel("Seek error", 
  97.                 "An IO error occured while seeking.",
  98.                 "OK",NULL,NULL);
  99.             return NULL;
  100.             }
  101.  
  102.         /* If we can't seek to the end because the file */
  103.         /* is too short, then */
  104.         /* seek to the end of file and add newlines*/            
  105.         fseek(source, 0L, SEEK_END);        
  106.         do
  107.             {
  108.             #ifdef DEBUG
  109.                 fprintf(stderr,"Looping: ftell(source) = "
  110.                     "%d \n",ftell(source));
  111.             #endif
  112.  
  113.             fputc('\n', source);
  114.             }
  115.         while( ftell(source) < FILE_LEN * here + 5);
  116.         
  117.         /* Try again */
  118.         if( fseek( source, (long) FILE_LEN * here, 0) == -1)         
  119.             {
  120.             fprintf(stderr, 
  121.             "%s: We have an error fseek()-ing to %d....\n"
  122.             "%s: Error Numbers are as follows: errno = %d, EIO =%d"
  123.             "%s: Calling procedure says: %s\n",
  124.             PROGNAME, here, 
  125.             PROGNAME, errno, EIO,
  126.             PROGNAME, errormsg);
  127.  
  128.             NXRunAlertPanel( "Seek error", errormsg, 
  129.                 "Quit", NULL, NULL);
  130.             fclose(source);
  131.             exit(1);
  132.             }
  133.         }
  134.     return source;
  135. }
  136.  
  137.